package com.ruoyi.buss.schedule.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.buss.customer.domain.DataCustomerInfo;
import com.ruoyi.buss.customer.service.IDataCustomerInfoService;
import com.ruoyi.buss.utils.BASE64DecodedMultipartFile;
import com.ruoyi.buss.utils.IdCardNetVerify;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ruoyi.buss.schedule.service.ScheduleService;
import com.ruoyi.buss.task.service.IDataTaskInfoService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.mock.web.MockMultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Component
public class ScheduleController {
	private static Logger logger = LoggerFactory.getLogger(ScheduleController.class);

	@Autowired
	private IDataTaskInfoService dataTaskInfoService;

	@Autowired
	private ScheduleService scheduleService;

	@Autowired
	private IDataCustomerInfoService dataCustomerInfoService;

	@Autowired
	private RemoteFileService remoteFileService;


	/**
	 * 根据营销方案自动更新关联客户信息
	 */
	@Scheduled(cron = "0 */1 * * * ?")
	public void release() {

		dataTaskInfoService.relationTaskCustomer();
	}

	/**
	 * 更新所有身份证号码正常客户的联网核查图片
	 */
	@Scheduled(cron = "0 */10 * * * ?")
	public void netCardPhoto() {
		/**
		 * 查询data_customer_info中身份证长度18的客户
		 * 联网核查图片路径为null
		 */
		List<DataCustomerInfo> list =
				dataCustomerInfoService.selectDataCustomerInfoListWithNetCard();

		for (DataCustomerInfo dataCustomerInfo:list) {
			String jsonStr = IdCardNetVerify
					.verify(dataCustomerInfo.getIdCard(), dataCustomerInfo.getName());

			if (org.apache.commons.lang3.StringUtils.isNotEmpty(jsonStr)) {
				JSONObject json = JSON.parseObject(jsonStr);
				String code = json.getString("code");
				if ("00".equals(code)) {
					String photo = json.getString("photo");

					InputStream inputStream = BASE64DecodedMultipartFile.base64ToMultipartOnt(photo);
					MultipartFile multipartFile = null;
					try {
						//文件名自定义，上传之后会重命名
						multipartFile = new MockMultipartFile("tmp.jpg",
								"tmp.jpg",
								"multipart/form-data", inputStream);
					} catch (IOException e) {
						logger.info("转换联网核查图片异常，返回=>{}",e.getMessage());
						e.printStackTrace();
					}
					R<SysFile> result = remoteFileService.uploadIdcardPhoto(multipartFile);

					if (Constants.SUCCESS.equals(result.getCode())) {
						String url = result.getData().getUrl();
						String msg = json.getString("msg");
						dataCustomerInfo.setNetIdCardUrl(url);
						dataCustomerInfo.setNetIdCardMsg(msg);
						/**
						 * 上传结束之后，保存图片路径
						 */
						dataCustomerInfoService
								.updateDataCustomerInfoWithNetCard(dataCustomerInfo);
					}

				} else {
					logger.info("联网核查失败，返回=>{}",jsonStr);
					String msg = json.getString("msg");
					dataCustomerInfo.setNetIdCardMsg(msg);
					/**
					 * 保存错误日志
					 */
					dataCustomerInfoService
							.updateDataCustomerInfoWithNetCard(dataCustomerInfo);
				}
			} else {
				continue;
			}


		}
	}
	
}
